package cn.com.nes.site.module.service.crm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cn.com.nes.common.util.BimUtils;
import cn.com.nes.site.entity.BimEnum;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.dao.crm.OpportunityDaoImpl;

@Service
public class OpportunityService {
    @Resource
    private OpportunityDaoImpl impl;

    public PageData getOpportunityList(DataReq req) {
        return impl.getOpportunityList(req);
    }

    public PageData getOpportunityDetail(DataReq req) {
        return impl.getOpportunityDetail(req);
    }

    public PageData saveOpportunity(DataReq req) {
        return impl.saveOpportunity(req);
    }
    
    public PageData delOpportunity(DataReq req) {
        return impl.delOpportunity(req);
    }
    
    public PageData opportunityLoss(DataReq req) {
        return impl.opportunityLoss(req);
    }
    

    
    public PageData getOpportunityDynamic(DataReq req) {
        return impl.getOpportunityDynamic(req);
    }
    
    public PageData saveOpportunityClaim(DataReq req) {
        return impl.saveOpportunityClaim(req);
    }
    
    public PageData delOpportunityClaim(DataReq req) {
        return impl.delOpportunityClaim(req);
    }
    
    public PageData saveOpportunityStaff(DataReq req) {
        return impl.saveOpportunityStaff(req);
    }
    
    public PageData delOpportunityStaff(DataReq req) {
        return impl.delOpportunityStaff(req);
    }
    
    public PageData addOpportunityFile(DataReq req) {
        return impl.addOpportunityFile(req);
    }
    
    public PageData delOpportunityFile(DataReq req) {
        return impl.delOpportunityFile(req);
    }
    
    public PageData addOpportunityQuote(DataReq req) {
        return impl.addOpportunityQuote(req);
    }
    
    public PageData delOpportunityQuote(DataReq req) {
        return impl.delOpportunityQuote(req);
    }
    
    public PageData getQuoteDetail(DataReq req) {
        return impl.getQuoteDetail(req);
    }
    
    public PageData transferOpportunity(DataReq req) {
        return impl.transferOpportunity(req);
    }
    
    public PageData getBimList(DataReq req) {
        return impl.getBimList(req);
    }
    
    public PageData delBim(DataReq req) {
        return impl.delBim(req);
    }
    
    public PageData delOpportunityBim(DataReq req) {
        return impl.delOpportunityBim(req);
    }
    
    public PageData addOpportunityBim(DataReq req) {
        return impl.addOpportunityBim(req);
    }
    
    //更新设计数据
    public void updateBimList(){
    	//获取设计列表
    	List<Map<String, Object>> designList = BimUtils.getDesignList();
    	for(Map<String,Object> design:designList){
    		HashMap<String,Object> map = new HashMap();
    		 String bimId = (String) design.get("dataId");
    		 Map<String,Object> meta = (Map<String,Object>) design.get("meta");
    		 String tid = (String) meta.get("tid");
    		 String uid = (String) meta.get("uid");
    		 String title = (String) meta.get("name");
    		 String pic = (String) meta.get("previewurl");
    		 String sharePermission = (String) meta.get("sharePermission");
    		 Map<String,Object> properties = (Map<String,Object>) meta.get("properties");
    		 String memo = (String) properties.get("comments");
    		 String shareUrl = "https://bim.zhuxingyun.com/viewer?id="+bimId;
    		 String designUrl = "https://bim.zhuxingyun.com/tool/design?id="+bimId+"&tid="+tid+"&uid="+uid;
    		map.put("bimId",bimId);
    		map.put("tid", tid);
    		map.put("uid", uid);
    		map.put("title", title);
    		map.put("pic", pic);
    		map.put("memo",memo);
    		map.put("designUrl", designUrl);
    		map.put("shareUrl", shareUrl);
    		map.put("sharePermission", sharePermission);
    		//查询dataId是否存在
    		DataReq req = new DataReq();
    		req.setReqData(map);
    		PageData res = impl.getBimList(req);
    		List<Map<String, Object>> bimList = (List<Map<String, Object>>) res.getPage();
    		if(bimList.size()<=0){
    			//插入bim
    			PageData res1 = impl.addBim(req);
    			System.out.println("insert success uuid:"+res1.getKey()+"--------");
    		}else {
    			//修改bim
    			PageData res1 = impl.updateBim(req);
    		}
    		
    	}
    }
    
    //更新设计清单
    @SuppressWarnings("unchecked")
	public void updateDesignDetail(DataReq req){
    	String url = "https://static.uzhujia.com/u/udesign/bom/";
    	List<Map<String, Object>> extData = null;
    	String bid = (String) req.getReqData().get("bimId");
    	if(!StringUtils.isEmpty(bid)){
    		extData = new ArrayList<Map<String,Object>>();
    		Map<String,Object> map = new HashMap();
    		map.put("bimId", bid);
    		extData.add(map);
    	}else{
    		PageData res = impl.getBimList(req);
    		extData = (List<Map<String, Object>>) res.getExtData();
    	}
    	for(Map<String, Object> bim:extData){
    		String bimId = (String) bim.get("bimId");
    		Map<String, Object> qd = BimUtils.getDesignQingdan(bimId);
    		if(qd==null) continue;
    		Map<String, Object> meta = (Map<String, Object>) qd.get("meta");
    		Map<String, Object> entireData = (Map<String, Object>) meta.get("entireData");
    		
    		//branches
    		List<Map<String,Object>> branches = (List<Map<String,Object>>) entireData.get("branches");
    		for(int i=0;branches!=null && i<branches.size();i++){
    			Map<String, Object> branche = branches.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "分支结构");
    			reqData.put("dataId", branche.get("dataId"));
    			Map<String,Object> branche_meta = (Map<String,Object>) branche.get("meta");
    			List<String> roomIds = (List<String>) branche_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("count",String.valueOf(branche_meta.get("count")));
    			reqData.put("icon",url+branche_meta.get("icon"));
    			reqData.put("name",BimEnum.getKeyVal("brancheName_"+branche_meta.get("name")));
    			reqData.put("spec",String.valueOf(branche_meta.get("spec")));
    			reqData.put("unit",String.valueOf(branche_meta.get("unit")));
    			if(impl.isExistDesign((String) branche.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//buildings
    		List<Map<String,Object>> buildings = (List<Map<String,Object>>) entireData.get("buildings");
    		for(int i=0;buildings!=null && i<buildings.size();i++){
    			Map<String, Object> building = buildings.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "建筑");
    			reqData.put("dataId", building.get("dataId"));
    			if(impl.isExistDesign((String) building.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//devices
    		List<Map<String,Object>> devices = (List<Map<String,Object>>) entireData.get("devices");
    		for(int i=0;devices!=null && i<devices.size();i++){
    			Map<String, Object> device = devices.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "设备");
    			reqData.put("dataId", device.get("dataId"));
    			Map<String,Object> device_meta = (Map<String,Object>) device.get("meta");
    			List<String> roomIds = (List<String>) device_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("brand",String.valueOf(device_meta.get("brand")));
    			reqData.put("lenX",String.valueOf(device_meta.get("lenX")));
    			reqData.put("lenY",String.valueOf(device_meta.get("lenY")));
    			reqData.put("lenZ",String.valueOf(device_meta.get("lenZ")));
    			reqData.put("name",String.valueOf(device_meta.get("name")));
    			reqData.put("icon",String.valueOf(device_meta.get("previewURL")));
    			reqData.put("price",String.valueOf(device_meta.get("price")));
    			reqData.put("style",String.valueOf(device_meta.get("style")));
    			if(impl.isExistDesign((String) device.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//hangers
    		List<Map<String,Object>> hangers = (List<Map<String,Object>>) entireData.get("hangers");
    		for(int i=0;hangers!=null && i<hangers.size();i++){
    			Map<String, Object> hanger = hangers.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "吊杆");
    			reqData.put("dataId", hanger.get("dataId"));
    			Map<String,Object> hanger_meta = (Map<String,Object>) hanger.get("meta");
    			List<String> roomIds = (List<String>) hanger_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("name","吊杆");
    			reqData.put("count",String.valueOf(hanger_meta.get("count")));
    			reqData.put("icon",url+hanger_meta.get("icon"));
    			reqData.put("unit",String.valueOf(hanger_meta.get("unit")));
    			if(impl.isExistDesign((String) hanger.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//holes
    		List<Map<String,Object>> holes = (List<Map<String,Object>>) entireData.get("holes");
    		for(int i=0;holes!=null && i<holes.size();i++){
    			Map<String, Object> hole = holes.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "墙洞");
    			reqData.put("dataId", hole.get("dataId"));
    			Map<String,Object> hole_meta = (Map<String,Object>) hole.get("meta");
    			List<String> roomIds = (List<String>) hole_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("levelId",String.valueOf(hole_meta.get("levelId")));
    			reqData.put("name","墙洞");
    			reqData.put("count",String.valueOf(hole_meta.get("count")));
    			reqData.put("icon",url+hole_meta.get("icon"));
    			reqData.put("spec",String.valueOf(hole_meta.get("spec")));
    			reqData.put("unit",String.valueOf(hole_meta.get("unit")));
    			if(impl.isExistDesign((String) hole.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//insulationPipes
    		List<Map<String,Object>> insulationPipes = (List<Map<String,Object>>) entireData.get("insulationPipes");
    		for(int i=0;insulationPipes!=null && i<insulationPipes.size();i++){
    			Map<String, Object> insulationPipe = insulationPipes.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "保温管");
    			reqData.put("dataId", insulationPipe.get("dataId"));
    			Map<String,Object> insulationPipe_meta = (Map<String,Object>) insulationPipe.get("meta");
    			List<String> roomIds = (List<String>) insulationPipe_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("length",String.valueOf(insulationPipe_meta.get("length")));
    			reqData.put("name",BimEnum.getKeyVal("pipeType_"+insulationPipe_meta.get("pipeType"))+BimEnum.getKeyVal("insulationPipeName_"+insulationPipe_meta.get("name")));
    			reqData.put("icon",url+insulationPipe_meta.get("icon"));
    			reqData.put("unit",String.valueOf(insulationPipe_meta.get("unit")));
    			reqData.put("pipeType",BimEnum.getKeyVal("pipeType_"+insulationPipe_meta.get("pipeType")));
    			if(impl.isExistDesign((String) insulationPipe.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//joints
    		List<Map<String,Object>> joints = (List<Map<String,Object>>) entireData.get("joints");
    		for(int i=0;joints!=null && i<joints.size();i++){
    			Map<String, Object> joint = joints.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "管件");
    			reqData.put("dataId", joint.get("dataId"));
    			Map<String,Object> joint_meta = (Map<String,Object>) joint.get("meta");
    			List<String> roomIds = (List<String>) joint_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("count",String.valueOf(joint_meta.get("count")));
    			reqData.put("icon",url+joint_meta.get("icon"));
    			reqData.put("name",BimEnum.getKeyVal("jointName_"+joint_meta.get("name")));
    			reqData.put("spec",String.valueOf(joint_meta.get("spec")));
    			reqData.put("unit",String.valueOf(joint_meta.get("unit")));
    			if(impl.isExistDesign((String) joint.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//levels
    		List<Map<String,Object>> levels = (List<Map<String,Object>>) entireData.get("levels");
    		for(int i=0;levels!=null && i<levels.size();i++){
    			Map<String, Object> level = levels.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "楼层");
    			reqData.put("dataId", level.get("dataId"));
    			Map<String,Object> level_meta = (Map<String,Object>) level.get("meta");
    			reqData.put("levelId",level_meta.get("buildingId"));
    			reqData.put("height",String.valueOf(level_meta.get("height")));
    			reqData.put("name",String.valueOf(level_meta.get("name")));
    			reqData.put("levelNo",String.valueOf(level_meta.get("levelNo")));
    			if(impl.isExistDesign((String) level.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//pipes
    		List<Map<String,Object>> pipes = (List<Map<String,Object>>) entireData.get("pipes");
    		for(int i=0;pipes!=null && i<pipes.size();i++){
    			Map<String, Object> pipe = pipes.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "管道");
    			reqData.put("dataId", pipe.get("dataId"));
    			Map<String,Object> pipe_meta = (Map<String,Object>) pipe.get("meta");
    			List<String> roomIds = (List<String>) pipe_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("diameter",String.valueOf(pipe_meta.get("diameter")));
    			reqData.put("icon",url+pipe_meta.get("icon"));
    			reqData.put("length",String.valueOf(pipe_meta.get("length")));
    			reqData.put("name",BimEnum.getKeyVal("pipeType_"+pipe_meta.get("type")));
    			reqData.put("materialType",BimEnum.getKeyVal("materialType_"+pipe_meta.get("materialType")));
    			reqData.put("shape",BimEnum.getKeyVal("shape_"+pipe_meta.get("shape")));
    			reqData.put("spec",String.valueOf(pipe_meta.get("spec")));
    			reqData.put("unit",String.valueOf(pipe_meta.get("unit")));
    			if(impl.isExistDesign((String) pipe.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//rooms
    		List<Map<String,Object>> rooms = (List<Map<String,Object>>) entireData.get("rooms");
    		for(int i=0;rooms!=null && i<rooms.size();i++){
    			Map<String, Object> room = rooms.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "房间");
    			reqData.put("dataId", room.get("dataId"));
    			Map<String,Object> room_meta = (Map<String,Object>) room.get("meta");
    			reqData.put("area",String.valueOf(room_meta.get("area")));
    			reqData.put("functionType",BimEnum.getKeyVal("roomType_"+room_meta.get("functionType")));
    			reqData.put("name",String.valueOf(room_meta.get("name")));
    			reqData.put("levelId",String.valueOf(room_meta.get("levelId")));
    			if(impl.isExistDesign((String) room.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//runways
    		List<Map<String,Object>> runways = (List<Map<String,Object>>) entireData.get("runways");
    		for(int i=0;runways!=null && i<runways.size();i++){
    			Map<String, Object> runway = runways.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "采暖");
    			reqData.put("dataId", runway.get("dataId"));
    			Map<String,Object> runway_meta = (Map<String,Object>) runway.get("meta");
    			List<String> roomIds = (List<String>) runway_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("name","采暖");
    			reqData.put("length",String.valueOf(runway_meta.get("length")));
    			reqData.put("deviceId",String.valueOf(runway_meta.get("manifold")));
    			reqData.put("spec",String.valueOf(runway_meta.get("spec")));
    			if(impl.isExistDesign((String) runway.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//textures
    		List<Map<String,Object>> textures = (List<Map<String,Object>>) entireData.get("textures");
    		for(int i=0;textures!=null && i<textures.size();i++){
    			Map<String, Object> texture = textures.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "贴砖环境");
    			reqData.put("dataId", texture.get("dataId"));
    			Map<String,Object> texture_meta = (Map<String,Object>) texture.get("meta");
    			List<String> roomIds = (List<String>) texture_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("area",String.valueOf(texture_meta.get("area")));
    			reqData.put("brand",String.valueOf(texture_meta.get("brand")));
    			reqData.put("name",String.valueOf(texture_meta.get("name")));
    			reqData.put("icon",texture_meta.get("previewURL"));
    			reqData.put("price",String.valueOf(texture_meta.get("price")));
    			reqData.put("series",String.valueOf(texture_meta.get("series")));
    			reqData.put("style",String.valueOf(texture_meta.get("style")));
    			reqData.put("unit",String.valueOf(texture_meta.get("unit")));
    			if(impl.isExistDesign((String) texture.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//wireses
    		List<Map<String,Object>> wireses = (List<Map<String,Object>>) entireData.get("wireses");
    		for(int i=0;wireses!=null && i<wireses.size();i++){
    			Map<String, Object> wirese = wireses.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", "强电");
    			reqData.put("dataId", wirese.get("dataId"));
    			Map<String,Object> wirese_meta = (Map<String,Object>) wirese.get("meta");
    			List<String> roomIds = (List<String>) wirese_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("circuit",BimEnum.getKeyVal("circuit_"+wirese_meta.get("circuit")));
    			reqData.put("length",String.valueOf(wirese_meta.get("length")));
    			reqData.put("icon",url+wirese_meta.get("icon"));
    			reqData.put("materialType",BimEnum.getKeyVal("materialType_"+wirese_meta.get("materialType")));
    			reqData.put("name",BimEnum.getKeyVal("wireseName_"+wirese_meta.get("name")));
    			reqData.put("spec",BimEnum.getKeyVal("spec_"+wirese_meta.get("spec")));
    			reqData.put("unit",String.valueOf(wirese_meta.get("unit")));
    			if(impl.isExistDesign((String) wirese.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    		//wirewes
    		List<Map<String,Object>> wirewes = (List<Map<String,Object>>) entireData.get("wirewes");
    		for(int i=0;wirewes!=null && i<wirewes.size();i++){
    			Map<String, Object> wirewe = wirewes.get(i);
    			req = new DataReq();
    			HashMap<String, Object> reqData = req.getReqData();
    			reqData.put("bimId", bimId);
    			reqData.put("type", " 弱电");
    			reqData.put("dataId", wirewe.get("dataId"));
    			Map<String,Object> wirewe_meta = (Map<String,Object>) wirewe.get("meta");
    			List<String> roomIds = (List<String>) wirewe_meta.get("roomIds");
    			reqData.put("roomIds",StringUtils.join(roomIds,","));
    			reqData.put("icon",url+wirewe_meta.get("icon"));
    			reqData.put("length",String.valueOf(wirewe_meta.get("length")));
    			reqData.put("name",BimEnum.getKeyVal("wireseName_"+wirewe_meta.get("name")));
    			reqData.put("unit",String.valueOf(wirewe_meta.get("unit")));
    			reqData.put("circuit",BimEnum.getKeyVal("weakWireType_"+wirewe_meta.get("weakWireType")));
    			if(impl.isExistDesign((String) wirewe.get("dataId"),bimId)){
    				impl.updateDesignDetail(req);
    			}else{
    				impl.addDesignDetail(req);
    			}
    		}
    	}
    	System.out.println("更新完毕!! ");
    }
    
    public PageData getDesignList(DataReq req) {
        return impl.getDesignList(req);
    }
    
    public PageData getLevelList(DataReq req) {
        return impl.getLevelList(req);
    }
    
    public PageData getRoomList(DataReq req) {
        return impl.getRoomList(req);
    }
    
    public static void main(String[] args) {
		String keyVal = BimEnum.getKeyVal("roomType_32");
		System.out.println(keyVal);
	}
    
    public PageData chageOpportunityStage(String opportunityId,String opportunitystage) {
        return impl.chageOpportunityStage(opportunityId,opportunitystage);
    }
}
